package no.ntnu.fp.views;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import no.ntnu.fp.comparators.CompareRoomReservations;
import no.ntnu.fp.console.Options;
import no.ntnu.fp.controllers.AppointmentController;
import no.ntnu.fp.model.Room;
import no.ntnu.fp.storage.DBConnection;

public class AppointmentView extends AbstractView {
	private String username;
	private AppointmentController appointmentController;
	private CompareRoomReservations crr;
	
	public AppointmentView(Options options) {
		System.out.println("Appointment view - konstruktør: " + username);
		appointmentController = new AppointmentController(options);
		crr = new CompareRoomReservations();
	}
	
	public void setUsername(String username) {
		this.username = username;
	}

	public void run() {
		int input;
		final int createAppointment = 0, editAppointment = 1, deleteAppointment = 2, tilbake = 3;
		do {
			String[] choices = {"Lage avtale", "Endre avtale", "Slette avtale", "Tilbake"};
			input = showChoices(choices, "Avtalemeny", "Avtale");
			
			switch (input) {
			case(createAppointment): createAppointment(); break;
			case(editAppointment): actionEditFromSelection(); break;
			case(deleteAppointment): actionDeleteFromSelection(); break;
			}
		} while (input != tilbake);
	}
	
	protected void createAppointment() {
		String date= inputDialog("Skriv inn avtaledato (maks 6 tegn):", "311212");
		String start = inputDialog("Skriv inn starttidspunkt for avtale:", "00.00");
		String stop = inputDialog("Skriv inn sluttidspunkt for avtale:", "00.00");
		String description = inputDialog("Skriv inn avtalebeskrivelse:");
		int numberOfParticipants= Integer.parseInt(inputDialog("Skriv inn totalt antall deltakere:"));
		String place;
		
		String bookRoom = inputDialog("Onsker du a reservere moterom? (Ja eller Nei)");
		if (bookRoom.equalsIgnoreCase("Ja")) {
			place = printRoomsExceptionHandler(numberOfParticipants, date, start, stop);
		}
		else
			place = inputDialog("Skriv inn sted for mote: ");

		appointmentController.actionCreate(username, date, start, stop, description, place, numberOfParticipants);
	}
	
	protected void actionEditFromSelection() {
		appointmentController.actionEditFromSelection(username);
	}

	protected void actionDeleteFromSelection() {
		System.out.println("Appointment view: " + username);
		appointmentController.actionDeleteFromSelection(username);
	}
	

	private String inputDialog(String message) {
		return JOptionPane.showInputDialog(message);
	}
	
	public static int intInputDialog(String message, int defaults) {
		return Integer.parseInt(JOptionPane.showInputDialog(message, defaults));
	}

	public static String inputDialog(String message, String defaults) {
		return JOptionPane.showInputDialog(message, defaults);
	}
	
	private String printRoomsExceptionHandler(int numberOfParticipants,
			String date, String start, String end) {
		try {
			return printRooms(numberOfParticipants,date,start,end);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String printRooms(int str, String date, String start, String stop) throws SQLException {
		
		String[] rooms = Room.getAllRoomsLargerThan(str);
		ArrayList<String> roomsFinal = new ArrayList<String>();
		
		//m� luke ut rom som allerede er reservert
		String timeString = date + " " + start + " " + stop;
		
		for (int i = 0; i < rooms.length; i++) {
			DBConnection db = new DBConnection();
			String query = "select Dato, Starttidspunkt, Sluttidspunkt from Avtale " +
					"where Sted = " + "\"" + rooms[i] + "\"" + "";
			ResultSet rs = db.makeSingleQuery(query);
			ArrayList<String> timeStrings = new ArrayList<String>();
			while (rs.next()) {
				String dato = rs.getString("Dato");
				String starttidspunkt = rs.getString("Starttidspunkt");
				String sluttidspunkt = rs.getString("Sluttidspunkt");
				String timeString2 = dato + " " + starttidspunkt + " " + sluttidspunkt;
				timeStrings.add(timeString2);
			}
			//hvis rom ikke allerede er reservert legges det inn i roomsFinal
			int compared = -1;
			for (int j = 0; j < timeStrings.size(); j++) {
				compared = crr.compare(timeString, timeStrings.get(j));
				if (compared == 0) { //er allerede reservert
					break; //bryter ut av lokka, ingen vits i a sjekke flere avtaler
				}
			}
			if (compared == -1) { //ikke reservert
				roomsFinal.add(rooms[i]);
			}
		}
		//konverter roomsFinal til roomsFinalArray
		String[] roomsFinalArray = new String[roomsFinal.size()];
		roomsFinal.toArray(roomsFinalArray);
		
		int input = Options.showChoices(roomsFinalArray, "Hvilket moterom vil du reservere?", "Moterom");
		
		return roomsFinalArray[input];
	}
}
